//
//  QHConfig.m
//  QHAudioConverterMan
//
//  Created by Anakin chen on 2021/4/14.
//

#import "QHConfig.h"

@implementation QHConfig

static QHConfig *this = nil;
static dispatch_once_t onceToken;

+ (instancetype)sharestanceWith:(NSString *)acc_name {
    dispatch_once(&onceToken, ^{
        this = [QHConfig new];
        this.acc_name = acc_name;
    });
    return this;
}

+ (instancetype)sharestance {
    dispatch_once(&onceToken, ^{
        this = [QHConfig new];
        this.acc_name = k_acc_video_44100;
    });
    return this;
}

- (AudioStreamBasicDescription)createAACAduioDes {
//    UInt32 bytesPerSample = sizeof(SInt32);
    UInt32 channels = _channels;
    
    AudioStreamBasicDescription audioDes ={0};
    audioDes.mSampleRate = _sampleRate;
    audioDes.mFormatID = kAudioFormatMPEG4AAC;
    audioDes.mFormatFlags = kMPEG4Object_AAC_LC;
    audioDes.mBytesPerPacket = 0;
    audioDes.mFramesPerPacket = 1024;
    audioDes.mBytesPerFrame = 0;
    audioDes.mChannelsPerFrame = channels;
    audioDes.mBitsPerChannel = 0;
    audioDes.mReserved = 0;
    
    return audioDes;
}

- (AudioStreamBasicDescription)createPCMAduioDes {
    UInt32 bytesPerSample = sizeof(SInt32);
    UInt32 channels = _channels;
    
    AudioStreamBasicDescription audioDes ={0};
    audioDes.mSampleRate = _sampleRate;
    audioDes.mFormatID = kAudioFormatLinearPCM;
    audioDes.mFormatFlags = kLinearPCMFormatFlagIsNonInterleaved | kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
    audioDes.mBytesPerPacket = bytesPerSample;
    audioDes.mFramesPerPacket = 1;
    audioDes.mBytesPerFrame = bytesPerSample;
    audioDes.mChannelsPerFrame = channels;
    audioDes.mBitsPerChannel = 8 * bytesPerSample;
    audioDes.mReserved = 0;
    
    return audioDes;
}

- (AudioStreamBasicDescription)createPCMMonoAudioSBDes {
    UInt32 bytesPerSample = sizeof(SInt32);
    UInt32 channels = 1;
    
    AudioStreamBasicDescription asbd;
    bzero(&asbd, sizeof(asbd));
    asbd.mSampleRate = _sampleRate;
    asbd.mFormatID = kAudioFormatLinearPCM;
    asbd.mFormatFlags = kLinearPCMFormatFlagIsNonInterleaved | kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
    asbd.mBitsPerChannel = 8 * bytesPerSample;
    asbd.mBytesPerFrame = bytesPerSample;
    asbd.mBytesPerPacket = bytesPerSample;
    asbd.mFramesPerPacket = 1;
    asbd.mChannelsPerFrame = channels;
    
    return asbd;
}

@end
